#include <iostream>
#include <algorithm>
#include <cstring>

using namespace std;

const int N = 410;

int n, k;
int p[N];
bool used[N];
struct Edge
{
    int a, b, w;

    bool operator< (const Edge & W) const{
        return w < W.w;
    }
}edge[N];


void init(){
    memset(used, 0, sizeof used);

    for(int i = 1; i <= n; i++){
        p[i] = i;
    }
}

int find(int x){
    if(p[x] != x){
        p[x] = find(p[x]);
    }

    return p[x];
}

int main(){
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);

    int res = 0;
    int cnt = 0;

    scanf("%d%d", &n, &k);

    init();
    
    for(int i = 0; i < k; i ++){
        int x, y, z;
        scanf("%d%d%d", &x, &y, &z);
        edge[i].a = x;
        edge[i].b = y;
        edge[i].w = z;
    }

    sort(edge, edge + k);

    for(int i = 0; i < k; i ++){
        int x = edge[i].a;
        int y = edge[i].b;
        int z = edge[i].w; 
        if (z == 0) continue;

        int a = find(x), b = find(y);
        if(a != b){
            cnt ++;
            p[a] = b;
            used[i] = true;
        }
    }

    for(int i = 0; i < k; i ++){
        if(!used[i]){
            res += edge[i].w;
        }
    }

    printf("%d", res);
    return 0;
}